//
//  NITabBar.m
//  gweAD
//
//  Created by navinfo air on 2016/11/24.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "NITabBar.h"
#import "ColorDefine.h"

@implementation NITabBar{
    
    __weak IBOutlet UIView *contentView;
    
    NSInteger selectIndex;
    
    __weak IBOutlet UIView *tabItem0;
    __weak IBOutlet UIView *tabItem1;
    __weak IBOutlet UIView *tabItem2;
    __weak IBOutlet UIView *tabItem3;
//    __weak IBOutlet UIView *tabItem4;
    
    __weak IBOutlet NSLayoutConstraint *indicatorViewConstraint;
    
}
+ (instancetype)NiTabbar{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
    
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    [self layout];
    
    selectIndex = -1;
    
}

- (IBAction)onButtonClick:(id)sender {
    NSInteger index = ((UIButton *)sender).tag - 100;
    [self setSelectIndex:index];
}

- (void)setSelectIndex:(NSInteger)index{
    if (index != selectIndex) {
        selectIndex = index;
        [self.delegate onTabSelect:index];
        
        [UIView animateWithDuration:0.2f animations:^{
            indicatorViewConstraint.constant = tabItem0.frame.size.width * index + (tabItem0.frame.size.width - 20)/2;
            [contentView setNeedsLayout];
            [contentView layoutIfNeeded];
        } completion:^(BOOL finished) {
            //变更选中状态（图片和文字）
            [self setTitlesColorAndImages];
        }];
    }
    //变更选中状态（图片和文字）
    [self setTitlesColorAndImages];
}

- (void)setTitlesColorAndImages{
    NSArray *tmpArray = @[tabItem0,tabItem1,tabItem2,tabItem3];
    UIView *tmpItem;
    for (int i = 0; i < [tmpArray count]; i++) {
        tmpItem = [tmpArray objectAtIndex:i];
        if (i == selectIndex) {
            [self setTitleColor:tmpItem isSelected:YES];
            [self setItemImage:tmpItem isSelected:YES];
        }else{
            [self setTitleColor:tmpItem isSelected:NO];
            [self setItemImage:tmpItem isSelected:NO];
        }
    }
}

- (void)setTitleColor:(UIView *)item isSelected:(BOOL)selected{
    UIView *tmpView = [item viewWithTag:100];
    UILabel *tmpLabel = [tmpView viewWithTag:1001];
    if (selected) {
        [tmpLabel setTextColor:DEF_COLOR_MAIN];
    }else{
        [tmpLabel setTextColor:DEF_COLOR_TEXT];
    }
}

- (void)setItemImage:(UIView *)item isSelected:(BOOL)selected{
    UIView *tmpView = [item viewWithTag:100];
    UIImageView *tmpImageView = [tmpView viewWithTag:1002];
    tmpImageView.highlighted = selected;
}

- (void)setHiddenTabItemAtIndex:(NSInteger)index hidden:(BOOL)hidden{
    switch (index) {
        case 0:
            tabItem0.hidden = hidden;
            break;
        case 1:
            tabItem1.hidden = hidden;
            break;
        case 2:
            tabItem2.hidden = hidden;
            break;
        case 3:
            tabItem3.hidden = hidden;
            break;
//        case 4:
//            tabItem4.hidden = hidden;
//            break;
            
        default:
            break;
    }
    [self layout];
}

- (void)layout{
    NSInteger count = 4;
    if (tabItem0.hidden) {
        count--;
    }
    if (tabItem1.hidden) {
        count--;
    }
    if (tabItem2.hidden) {
        count--;
    }
    if (tabItem3.hidden) {
        count--;
    }
//    if (tabItem4.hidden) {
//        count--;
//    }
    if (count == 0) {
        count = 1;
    }
    CGFloat itemWidth = [UIScreen mainScreen].bounds.size.width/count;
    if (tabItem0.hidden) {
        [tabItem0 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }else{
        [tabItem0 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(itemWidth);
        }];
    }
    if (tabItem1.hidden) {
        [tabItem1 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }else{
        [tabItem1 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(itemWidth);
        }];
    }
    if (tabItem2.hidden) {
        [tabItem2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }else{
        [tabItem2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(itemWidth);
        }];
    }
    if (tabItem3.hidden) {
        [tabItem3 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }else{
        [tabItem3 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(itemWidth);
        }];
    }
//    if (tabItem4.hidden) {
    //        [tabItem4 mas_updateConstraints:^(MASConstraintMaker *make) {
//    make.width.mas_equalTo(0);
//}];
//    }else{
    //        [tabItem4 mas_updateConstraints:^(MASConstraintMaker *make) {
//    make.width.mas_equalTo(itemWidth);
//}];
//    }
//    [self setNeedsLayout];
//    [self layoutIfNeeded];
}





@end
